/*
 * @Author: wwssaabb
 * @Date: 2021-09-06 14:40:02
 * @LastEditTime: 2021-10-29 16:30:10
 * @FilePath: \handwritten-code\demo\函数柯理化.js
 */

/* 
  什么是函数柯理化？
    柯理化是把接受多个参数的函数变换成接受一个单一参数的函数，整合多次传入的参数并返回
    结果的新函数的技术
  实现求和柯理化函数 add(1,2)(3,4)(5,6)(7,8)(9,10)=55
*/

function curry() {
  let _arg = [...arguments]

  function res() {
    _arg.push(...arguments)
    return res
  }
  res.toString = function () {
    console.log(_arg.reduce((p, c) => p + c))
  }
  return res
}

curry(1, 2)(3, 4)(5, 6)(7, 8)(9, 10).toString()

function currying() {
  let args = Array.prototype.slice.call(arguments)

  let fn = function () {
    args.push(...Array.prototype.slice.call(arguments))
    return fn
  }

  fn.toString = () => console.log(args.reduce((x, y) => x + y))
  fn.valueOf = () => args.reduce((x, y) => x + y)
  return fn()
}